// pages/programme/select-material/selectMaterial.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    materialType:[],
    allSelectList: [],
    page: {
      currentPage: 1,
      pageSize: 10,
      total: 0,
    },
    type: 'add',
    isNotdate: false,
    productId: '',
    productName: '',
    customerId: '',
    id: '',
    userName: '',
    presentationClassId: '1',
    customerList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, options.productId === 'undefined')
    wx.setNavigationBarTitle({
      title: '选择素材'
    })
    let allSelectList = JSON.parse(decodeURIComponent(options.allSelectList))
    this.setData({
      allSelectList: allSelectList,
      type: options.type,
      navBarHeight: app.globalData.navBarHeight,
      statusBarHeight: app.globalData.statusBarHeight,
    })
    this.setData({
      productName: options.productName,
      productId: options.productId,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      'page.currentPage': 1
    })
    this.getMaterialClassList()
    this.getPresentationList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  reback () {
    var allSelectList = []
    let code = this.data.productId ? '03-00' : '03-01'
    wx.redirectTo({
      url: `/pages/programme/create-programme/createProgramme?sensors_ext=${code}&allSelectList=${allSelectList}&type=${this.data.type}&customerId=${this.data.customerId}&productName=${this.data.productName}&id=${this.data.id}&userName=${this.data.userName}&productId=${this.data.productId}`
    })
  },
  save () {
    console.log(this.data.allSelectList)
    var allSelectList = encodeURIComponent(JSON.stringify(this.data.allSelectList))
    let code = this.data.productId ? '03-00' : '03-01'
    if (this.data.allSelectList.length > 0) {
      wx.redirectTo({
        url: `/pages/programme/create-programme/createProgramme?sensors_ext=${code}&type=${this.data.type}&customerId=${this.data.customerId}&productName=${this.data.productName}&id=${this.data.id}&userName=${this.data.userName}&productId=${this.data.productId}&allSelectList=${allSelectList}`
      })
    } else {
      wx.showToast({
        title: '请选择演示素材',
        icon: 'none',
        duration: 2000
      })
    }
  },
  getMaterialClassList () {
    app.api.post({
      url: 'presentationClass_list',
      data: {}
    }).then(res => {
      let arr = res
      arr.unshift({id: '1',presentationClassName: '全部'})
      arr.push({id: '0',presentationClassName: '其他'})
      this.setData({
        materialType: arr
      })
      console.log(res)
    })
  },
  toggleType (e) {
    let { id } = e.currentTarget.dataset
    this.data.materialType.forEach((v, k) => {
      v.active = id=== v.materialClassId
    })
    this.setData({
      materialType: this.data.materialType,
      presentationClassId: id,
      'page.currentPage': 1
    })
    this.getPresentationList()
  },
  startProgramme (e) {
    let presentationIds = [e.currentTarget.dataset.item.id]
    let code = this.data.productId ? '03-00' : '03-01'
    if (e.currentTarget.dataset.item.materialType === 'link') {
      wx.navigateTo({ url: `/pages/home-page/watch-project/watchProject?sensors_ext=${code}&url=${encodeURIComponent(e.currentTarget.dataset.item.url)}` })
    } else {
      wx.navigateTo({
        url: `/pages/home-page/to-demonstration/toDemonstration?from=material&customerId=${this.data.customerId}&sensors_ext=${code}&type=demonstration&presentationIds=${presentationIds}&save=false`
      }) 
    }
  },
  toggle (e) {
    let { id } = e.currentTarget.dataset.item
    this.data.customerList.forEach((v, k) => {
      if (id === v.id) {
        if (v.active) {
          console.log(1, v, this.data.allSelectList)
          v.active = false
          if (this.data.allSelectList.length > 0) {
            this.data.allSelectList.forEach((j, g) => {
              console.log(v, j)
              if (v.presentationName === j.presentationName) {
                this.data.allSelectList.splice(g, 1)
              }
            })
          }
        } else {
          v.active = true
          this.data.allSelectList.push(v)
        }
      }
    })
    console.log(this.data.allSelectList)
    this.setData({
      customerList: this.data.customerList,
      allSelectList: this.data.allSelectList
    })
  },
  loadMore () {
    console.log('加载了更多')
    const { page, customerList } = this.data
    if (page.total > customerList.length) {
      this.setData({
        'page.currentPage': page.currentPage + 1,
        'isLoadMore': true
      })
      this.getPresentationList()
    }
  },
  getPresentationList: function () {
    let { page, customerList, presentationClassId } = this.data
    const params = {
      currentPage: page.currentPage,
      pageSize: page.pageSize,
      presentationClassId
    }
    app.api.post({
      url: 'presentation_page',
      data: params
    }).then(res => {
      let arr = []
      if (res.list.length > 0) {
        arr = res.list
        for (let i = 0; i < arr.length; i++) {
          for (let j = 0; j < this.data.allSelectList.length; j++) {
            if (this.data.allSelectList[j].id === arr[i].id) {
              arr[i].active = true
            }
          }
        }
        if (this.data.isLoadMore) {
          console.log('执行加载')
          customerList.push(...arr)
        } else {
          console.log('执行加载2')
          customerList = arr
        }
        console.log(customerList)
        this.setData({
          customerList,
          isLoadMore: false,
          isNotdate: false,
          'page.total': Number(res.total)
        })
      } else {
        this.setData({
          customerList: [],
          isNotdate: true
        })
      }
    })
  },
})